<?php

namespace app\admin\controller\system;

use app\admin\controller\Base;
use think\Db;

/**
 * 菜单规则管理
 * Class RuleController
 * @package app\admin\controller\auth
 */
class Config extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->assign('group', config('param.config_group_list'));
        $this->assign('type', config('param.config_type_list'));
    }

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }

    /**
     * 列表
     * @return mixed
     */
    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();

        $data['count'] = Db::name('config')
            ->where($map)
            ->count();
        $data['data'] = Db::name('config')
            ->where($map)
            ->order('sort desc')
            ->select();
$type=config('param.config_type_list');
$group=config('param.config_group_list');
        foreach ($data['data'] as &$info) {
            $info['update_time'] = $info['update_time'] ? date('Y-m-d H:i:s', $info['update_time']) : '-';
$info['type']=@$type[$info['type']];
$info['group']=@$group[$info['group']];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    //配置
    public function group()
    {
        if ($this->request->isPost()) {
            $config = $this->request->param('config/a');
            if ($config) {
                foreach ($config as $name => &$value) {
                    if(is_array($value)){
                        $temp='';
                        foreach ($value as $key=>$info){
                            $temp.=$key.':'.$info."\n";
                        }
                        $value=$temp;

                    }
                    Db::name('config')->where(['name' => $name])->setField('value', $value);
                }
            }
            cache('sys:cache:config', null);
            $this->success('保存成功！');
        } else {
            //所有配置项
            $group = config('param.config_group_list');
            $list = [];
            foreach ($group as $key => $val) {
                $temp = [];
                $temp['id'] = $key;
                $temp['name'] = $val;
                $temp['list'] = Db::name('config')->where(['lock' => 0, 'group' => $key])->select();
                foreach ($temp['list'] as &$item) {
                    if ($item['type'] == 3) {
                        $item['value'] = $this->parse(3, $item['value']);
                    }
                }
                $list[] = $temp;
            }
//            dump($list);
//            exit;
            $this->assign('list', $list);
            $this->assign('group', $group);
            $this->assign('type', config('param.config_type_list'));
            return view();
        }

    }


    /**
     * 根据配置类型解析配置
     * @param $type
     * @param $value
     * @return array|array[]|false|string[]
     */
    private static function parse($type, $value)
    {
        switch ($type) {
            case 3: //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if (strpos($value, ':')) {
                    $value = [];
                    foreach ($array as $val) {
                        list($k, $v) = explode(':', $val);
                        $value[$k] = $v;
                    }
                } else {
                    $value = $array;
                }
                break;
        }
        return $value;
    }


    /**
     * 添加页面和添加操作
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (Db::name('config')->insert($post) !== false) {
                cache('sys:cache:config', null);
                $this->success('操作成功');
            }
            $this->error('添加失败');
        } else {
            return view();
        }
    }

    /**
     * 修改页面和修改操作
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (Db::name('config')->update($post) !== false) {
                cache('sys:cache:config', null);
                $this->success('修改成功');
            }
            $this->error('修改失败');
        } else {
            $post = $this->request->param();
            $id = $this->request->param('id', 0, 'intval');
            if (!$id) {
                exit('参数错误');
            }
            $info = Db::name('config')->where('id', $id)->find();
            $this->assign('info', $info);
            return view();
        }
    }

    /**
     * 快速排序
     * @return json
     */
    public function sort()
    {
        $id = $this->request->param('id', 0, 'intval');
        $sort = $this->request->param('sort', 0, 'intval');

        if ($id > 0 && Db::name('config')->where(['id' => $id])->setField('sort', $sort) !== false) {
            $this->success('设置成功');
        }
        $this->error('更新失败');
    }

    /**
     * 快速锁定
     * @return json
     */
    public function lock()
    {
        $id = $this->request->param('id', 0, 'intval');
        $lock = $this->request->param('lock', 0, 'intval');
        if ($id > 0 && Db::name('config')->where(['id' => $id])->setField('lock', $lock) !== false) {
            $this->success('设置成功');
        }
        $this->error('更新失败');
    }

    /**
     * 删除操作
     * @return json
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id <= 0) {
            $this->error('参数错误');
        }
        if (Db::name('config')->delete($id) !== false) {
            cache('sys:cache:config', null);
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

}